package com.wuxianedu.bitmapdrawable;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

/**
 * Created by TED on 2016/9/28.
 */
public class DrawActivity extends AppCompatActivity implements View.OnClickListener {

    private Button bonds;
    private ImageView iv;
    private Button green;
    private Button red;
    private Button save;
    private float startX;
    private float startY;
    private Canvas canvas;
    private Paint paint;
    private Bitmap bitmapCopy;
    private int width;
    private int height;
    private int width1 = 7;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_draw);
        init();
        width = getWindowManager().getDefaultDisplay().getWidth();
        height = getWindowManager().getDefaultDisplay().getHeight();

        // 这里得到的bitmap是只读的
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);

        // 用Bitmap.createBitmap创建出来的bitmap才是可写的 这里还是白纸
        bitmapCopy = Bitmap.createBitmap(width, height, bitmap.getConfig());



        // 需要一个画笔
        paint = new Paint();
        // 需要一个画板
        canvas = new Canvas(bitmapCopy);
        // 把原图画到白纸上
        canvas.drawBitmap(bitmap, new Matrix(), paint);

        // 给画笔设置颜色
        paint.setColor(Color.RED);
        // 给画笔设置宽度
        paint.setStrokeWidth(10);
        // 作画
        canvas.drawLine(10, 10, 300, 300, paint);
        // 画完之后的图片再去显示到iv
        iv.setImageBitmap(bitmapCopy);
        iv.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:

                        startX = event.getX();
                        startY = event.getY();
                        Log.e("-----", "anxia"+startX+","+startY);
                        break;

                    case MotionEvent.ACTION_MOVE:

                        float x = event.getX();
                        float y = event.getY();
                        canvas.drawLine(startX, startY, x, y, paint);
                        startX = x;
                        startY = y;
                        Log.e("-----", "haudong"+x+","+y);
                        // 画完更新 画板
                        iv.setImageBitmap(bitmapCopy);
                        break;
                }
                // true 消费事件（自己处理这个事件） false 代表 不消费，给父控件处理
                return true;
            }
        });
    }


    private void init() {
        iv = (ImageView) findViewById(R.id.iv_huaban);
        bonds = (Button) findViewById(R.id.add_bonds);
        green = (Button) findViewById(R.id.add_green);
        red = (Button) findViewById(R.id.add_red);
        save = (Button) findViewById(R.id.save);
        bonds.setOnClickListener(this);
        green.setOnClickListener(this);
        red.setOnClickListener(this);
        save.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.add_bonds:
                width1 +=3;
                paint.setStrokeWidth(width1);
                break;
            case R.id.add_green:
                paint.setColor(Color.GREEN);
                break;
            case R.id.save:
                // 通过流去保存
                break;
        }
    }
}
